from PyQt5.QtWidgets import QDialog

# 导入窗体内容
from design.folderNameEditDlg import *

from common.logger import logger
from common.retcode import RetCode
from common.utils import center_window_on_screen
from api.folder import FolderAction

# 创建 MainWin 类并传入Ui_MainWindow
class FolderNameEditDlg(QDialog, Ui_FolderNameEditDlg):
	def __init__(self, parent=None):
		super(FolderNameEditDlg, self).__init__(parent)
		self.setupUi(self)
		center_window_on_screen(self)

		self.pushButtonSubmit.clicked.connect(self.onClickedSubmit)

		self._sess_wp = None
		self._cur_fid = None
		self._is_rename = False

	def reset(self, sess_wp, cur_fid, is_rename=False, cur_folder_name=''):
		self._sess_wp = sess_wp
		self._cur_fid = cur_fid
		self._is_rename = is_rename
		if is_rename:
			self.setWindowTitle('重命名')
			self.lineEditFoldName.setText(cur_folder_name)
		else:
			self.setWindowTitle('新建文件夹')
			self.lineEditFoldName.setText('')
		self.pushButtonSubmit.setEnabled(True)

	def onClickedSubmit(self):
		self.pushButtonSubmit.setEnabled(False)

		fold_name = self.lineEditFoldName.text()
		folder_action = FolderAction(self._sess_wp)

		if self._is_rename:
			code = folder_action.rename(self._cur_fid, fold_name)
			if code != RetCode.SUCCESS:
				print(f'rename fail: {code}')
				self.reject()
			else:
				logger.debug('rename success')
				self.accept()
		else:
			code, fid = folder_action.mkdir(self._cur_fid, fold_name)
			if code != RetCode.SUCCESS:
				print(f'mkdir fail: {code}')
				self.reject()
			else:
				logger.debug(f'mkdir success, fid: {fid}')
				self.accept()
		
			
